package com.xiaode.controller.app.task.ordinary;

import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.ext.cors.EnableCORS;
import com.jfinal.kit.Ret;
import com.xiaode.common.anotation.BooleanV;
import com.xiaode.common.anotation.JSONArrayString;
import com.xiaode.util.apidoc.annotation.Api;
import com.xiaode.util.apidoc.annotation.ApiOper;
import com.xiaode.util.apidoc.annotation.ApiPara;
import javax.validation.constraints.Size;

@Path("/ordinary")
@Api("普通任务模块")
@EnableCORS
public class OrdinaryController extends Controller {
	
    @Inject
    OrdinaryService service;

    @ApiOper("发布任务")
    public void publishTask(
    		@Size(max = 500) @ApiPara(value = "content", notes = "任务内容") String content,
    		@Size(max = 200) @ApiPara(value = "title", notes = "任务标题") String title,
            @JSONArrayString @ApiPara(value = "pictures", notes = "图片") String pictures,
            @Size(min = 1,max = 10) @ApiPara(value = "score", notes = "任务总分") Integer score) throws Exception {
    	Integer accountId = getAttr("accountId");
    	Integer userTypeMy = getAttr("userType");
        Ret ret = service.publishTask(accountId, userTypeMy, content, title, pictures, score);
        renderJson(ret);
    }

    @ApiOper("修改任务")
    public void updateTask(
    		@Size @ApiPara(value = "taskId", notes = "任务Id") Integer taskId,
    		@Size(max = 500) @ApiPara(value = "content", notes = "任务内容") String content,
    		@Size(max = 200) @ApiPara(value = "title", notes = "任务标题") String title,
    		@ApiPara(value = "pictures", notes = "图片") String pictures,
    		@Size(min = 1) @ApiPara(value = "score", notes = "任务总分") Integer score,
    		@ApiPara(value = "isDel", notes = "是否删除") Boolean isDel) throws Exception {
    	Integer accountId = getAttr("accountId");
    	Integer userTypeMy = getAttr("userType");
        Ret ret = service.updateTask(accountId, userTypeMy, taskId, content, title, pictures, score, isDel);
        renderJson(ret);
    }

    @ApiOper("查询老师发布的任务。注：所有学生都可以做任务，但是审核任务是自己的老师审核")
    public void findTaskPage(
    		@BooleanV @ApiPara(value = "isMy", notes = "是否是老师自己查看我的发布") Boolean isMy,
    		@Size @ApiPara(value = "page", notes = "当前页") Integer page,
    		@Size @ApiPara(value = "limit", notes = "每页大小") Integer limit) throws Exception {
        Integer accountId = getAttr("accountId");
        Integer userType = getAttr("userType");
        Ret ret = service.findTaskPage(accountId, userType, isMy, page, limit);
        renderJson(ret);
    }

    @ApiOper("学生个人页面中查看学生已审核的任务")
    public void findUserTaskPage(
            @Size @ApiPara(value = "accountId", notes = "账号id") Integer accountId,
            @Size @ApiPara(value = "page", notes = "当前页") Integer page,
            @Size @ApiPara(value = "limit", notes = "每页大小") Integer limit) throws Exception {
        Ret ret = service.findUserTaskPage(accountId, page, limit);
        renderJson(ret);
    }

    @ApiOper("提交任务")
    public void commitTask(
    		@Size @ApiPara(value = "taskId", notes = "任务Id") Integer taskId,
            @Size(max = 500) @ApiPara(value = "content", notes = "任务内容") String content,
            @JSONArrayString @ApiPara(value = "files", notes = "files[{type:xxx,url:xxx},{type:xxx,url:xxx}]") String files) throws Exception {
    	Integer accountId = getAttr("accountId");
        Ret ret = service.commitTask(accountId, taskId, content, files);
        renderJson(ret);
    }

    @ApiOper("查询任务详情")
    public void findTaskDetails(@Size @ApiPara(value = "taskId",notes = "任务id")Integer taskId)throws Exception{
    	Integer accountId = getAttr("accountId");
        Ret ret = service.findTaskDetails(accountId, taskId);
        renderJson(ret);
    }
    
    @ApiOper("轨迹中查询普通任务详情")
    public void findTaskDetailsInTrajectory(@Size @ApiPara(value = "taskUserId",notes = "任务userid")Integer taskUserId)throws Exception{
        Ret ret = service.findTaskDetailsInTrajectory(taskUserId);
        renderJson(ret);
    }
    
    @ApiOper("普通任务审核")
    public void examine(@Size @ApiPara(value = "taskId", notes = "任务Id") Integer taskId,
            @Size(min = 1, max = 10) @ApiPara(value = "score", notes = "任务得分") Integer score,
            @ApiPara(value = "content", notes = "评论内容") String content,
            @BooleanV @ApiPara(value = "addTrajectory",notes = "是否加入轨迹图")Boolean addTrajectory) throws Exception {
    	Integer accountId = getAttr("accountId");
    	Integer userType = getAttr("userType");
    	Ret ret = service.examine(accountId, userType, taskId, score, content, addTrajectory);
    	renderJson(ret);
    }
    

}
